package cn.gene.timeyan.article.webapi.config;

import cn.gene.timeyan.article.webapi.job.ArticleJob;
import cn.gene.timeyan.article.webapi.job.ArticleListJob;
import cn.gene.timeyan.article.webapi.job.CommentJob;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.util.Objects;

@Configuration
public class QuartzConfiguration {
    @Autowired
    private MyAdaptableJobFactory myAdaptableJobFactory;
    @Bean
    public SchedulerFactoryBean createSchedulerFactoryBean(@Qualifier("articleJobTrigger") Trigger articleJobTrigger,
                                                           @Qualifier("articleListJobTrigger")Trigger articleListJobTrigger,
                                                           @Qualifier("commentJobTrigger")Trigger commentJobTrigger
                                                           ) {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();

        // 使用自定义的job工厂,交由spring的IOC
        schedulerFactoryBean.setJobFactory(myAdaptableJobFactory);
        schedulerFactoryBean.setTriggers(articleJobTrigger,
                articleListJobTrigger,
                commentJobTrigger
                );
        return schedulerFactoryBean;
    }
    @Bean("articleJobDetail")
    public JobDetailFactoryBean articleJobDetail(){
        JobDetailFactoryBean bean=new JobDetailFactoryBean();
        bean.setJobClass(ArticleJob.class);
        return bean;
    }
    @Bean("articleJobTrigger")
    public CronTriggerFactoryBean articleJobTrigger(@Qualifier("articleJobDetail") JobDetailFactoryBean articleJobDetail) {
        CronTriggerFactoryBean bean=new CronTriggerFactoryBean();
        bean.setJobDetail(Objects.requireNonNull(articleJobDetail.getObject()));
        //每三十分钟执行一次
        bean.setCronExpression("0 0/30 * * * ? ");
//        bean.setCronExpression("0/1 * * * * ? ");
        return bean;
    }
    @Bean(name = "articleListJobDetail")
    public JobDetailFactoryBean articleListJobDetail(){
        JobDetailFactoryBean bean=new JobDetailFactoryBean();
        bean.setJobClass(ArticleListJob.class);
        return bean;
    }
    @Bean(name = "articleListJobTrigger")
    public CronTriggerFactoryBean articleListJobTrigger(@Qualifier("articleListJobDetail")JobDetailFactoryBean articleListJobDetail) {
        CronTriggerFactoryBean bean=new CronTriggerFactoryBean();
        bean.setJobDetail(Objects.requireNonNull(articleListJobDetail.getObject()));
        //每三小时执行一次
        bean.setCronExpression(" 0 0 0/3 * * ?");
        return bean;
    }
    @Bean(name = "commentJobDetail")
    public JobDetailFactoryBean commentJobDetail(){
        JobDetailFactoryBean bean=new JobDetailFactoryBean();
        bean.setJobClass(CommentJob.class);
        return bean;
    }
    @Bean(name = "commentJobTrigger")
    public CronTriggerFactoryBean commentJobTrigger(@Qualifier("commentJobDetail")JobDetailFactoryBean commentJobDetail) {
        CronTriggerFactoryBean bean=new CronTriggerFactoryBean();
        bean.setJobDetail(Objects.requireNonNull(commentJobDetail.getObject()));
        bean.setCronExpression("0 0/30 * * * ? ");
//        bean.setCronExpression("0/5 * * * * ?");
        return bean;
    }

    /*private static final String LIKE_TASK_IDENTITY = "LikeTaskQuartz";
    LikeTask likeTask=new LikeTask();

    @Bean
        public JobDetail likeTaskDetail() {
        return JobBuilder.newJob(likeTask.getClass())
                .withIdentity(LIKE_TASK_IDENTITY)
                .storeDurably()
                .build();
    }
    @Bean
    public Trigger likeTaskTrigger() {
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(20) //设置时间周期单位秒
                .withIntervalInMinutes(10)// 十分钟
                .repeatForever();
        return TriggerBuilder.newTrigger()
                .forJob(likeTaskDetail())
                .withIdentity(LIKE_TASK_IDENTITY)
                .withSchedule(scheduleBuilder)
                .build();
    }*/
}
